package com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;


/**
 * Created by Administrator on 2018/8/9.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class BitmapUtils {
    /**
     * @desc 将图片转成圆角的RoundedBitmapDrawable
     * @param resId         图片资源
     * @param round         圆角
     * @param mContext      上下文
     * */
    public static RoundedBitmapDrawable rectRoundBitmap(int resId,int round, Context mContext){
        //得到资源文件的BitMap
        Bitmap image= BitmapFactory.decodeResource(mContext.getResources(), resId);
        //创建RoundedBitmapDrawable对象
        RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(mContext.getResources(),image);
        //抗锯齿
        roundImg.setAntiAlias(true);
        //设置圆角半径
        roundImg.setCornerRadius(round);
        return  roundImg;
    }
    /**
     * @desc 将图片转成圆的RoundedBitmapDrawable
     * @param resId         图片资源
     * @param mContext      上下文
     * */
    public static RoundedBitmapDrawable roundBitmap(int resId, Context mContext){
        //如果是圆的时候，我们应该把bitmap图片进行剪切成正方形， 然后再设置圆角半径为正方形边长的一半即可
        Bitmap image = BitmapFactory.decodeResource(mContext.getResources(), resId);
        Bitmap bitmap = null;
        //将长方形图片裁剪成正方形图片
        if (image.getWidth() == image.getHeight()) {
            bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
        } else {
            bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
        }
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
        //圆角半径为正方形边长的一半
        roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
        //抗锯齿
        roundedBitmapDrawable.setAntiAlias(true);
        return roundedBitmapDrawable;
    }
    /**
     * @desc 根据本地图片路径转成Bitmap对象
     *
     * */
    public static Bitmap imgToBitmap(String path){
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
        } catch (FileNotFoundException e) {

        }
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
        return bitmap;
    }
    /**
     * 将Bitmap转换成Base64字符串
     * @param bit
     * @return
     */
    public static String Bitmap2StrByBase64(Bitmap bit){
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 100, bos);//参数100表示不压缩
        byte[] bytes=bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }
}
